<template>
  <div class="content">
    <!-- 顶部搜索栏 -->
    <div class="top">
      <a-card :bordered="false">
        <a-space :size="20">
          <a-popconfirm
            title="确认删除？"
            ok-text="确认"
            cancel-text="取消"
            @confirm="removeBachById"
            @cancel="cancel"
          >
            <a-button :disabled="!hasSelected" type="primary" danger ghost
              ><DeleteOutlined />批量删除</a-button
            >
          </a-popconfirm>
        </a-space>
      </a-card>
    </div>

    <!-- 表格数据展示 -->
    <div class="table">
      <a-card :bordered="false">
        <div>
          <a-spin tip="加载中" :spinning="false">
            <a-table
              :row-selection="{
                selectedRowKeys: state.selectedRowKeys,
                onChange: onSelectChange,
              }"
              :data-source="data"
              :pagination="false"
              :loading="loading"
            >
              <a-table-column
                fixed="left"
                key="avatar"
                title="头像"
                data-index="avatar"
              >
                <template #default="{ record }">
                <a-avatar :src="record.avatar" shape="square" size="large">
                  <template #icon><UserOutlined /></template>
                </a-avatar>
              </template>
              </a-table-column>
              <a-table-column
                fixed="left"
                key="nickName"
                title="用户昵称"
                data-index="nickName"
                :ellipsis="true"
              />
              <a-table-column
                :ellipsis="true"
                key="content"
                title="反馈内容"
                data-index="content"
              />
              <a-table-column fixed="right" key="id" title="操作">
                <template #default="{ record }">
                  <div style="display: flex">
                    <div>
                      <a-button @click="edit(record.key)" type="link" ghost
                        >修改</a-button>
                    </div>
                    <div style="margin-left: 10px">
                      <a-popconfirm
                        title="确认删除？"
                        ok-text="确认"
                        cancel-text="取消"
                        @confirm="remove(record.key)"
                        @cancel="cancel"
                      >
                        <a-button type="link" ghost danger>
                         删除
                        </a-button>
                      </a-popconfirm>
                    </div>
                  </div>
                </template>
              </a-table-column>
            </a-table>
          </a-spin>
        </div>
        <!-- 分页 -->
        <div
          style="
            height: 40px;
            width: 100%;
            margin-top: 30px;
            display: flex;
            justify-content: center;
          "
        >
          <a-pagination
            v-model:current="pageNo"
            v-model:page-size="pageSize"
            :total="total"
            @change="handlePageChange"
            :show-total="(total) => `共 ${total} 条`"
          />
        </div>
      </a-card>
    </div>

    <!-- 修改弹出框 -->
    <div>
      <a-modal
        v-model:open="open"
        @cancel="handleCancel"
        title="修改反馈"
        :footer="null"
      >
        <div style="padding: 30px 0 20px 50px">
          <a-form
            ref="formRef"
            :model="feedback"
            :rules="rules"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
          >
            <a-form-item ref="nickName" label="用户昵称" name="nickName">
              <a-input
                :disabled="true"
                maxlength="18"
                v-model:value="feedback.nickName"
                size="large"
              />
            </a-form-item>

            <a-form-item ref="avatar" label="头像" name="avatar">
              <a-image
                :preview="false"
                style="border-radius: 10%"
                :width="80"
                :src="feedback.avatar"
              />
            </a-form-item>
            <a-form-item ref="content" label="内容" name="content">
              <a-textarea
                size="large"
                v-model:value="feedback.content"
                :rows="4"
              />
            </a-form-item>
            <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
              <a-button type="primary" @click="save">保存</a-button>
              <a-button style="margin-left: 10px" @click="handleCancel"
                >取消</a-button
              >
            </a-form-item>
          </a-form>
        </div>
      </a-modal>
    </div>
  </div>
</template>

<script setup>
import { DeleteOutlined,} from "@ant-design/icons-vue";
import { ref, reactive, computed, onMounted } from "vue";
import { message } from "ant-design-vue";
import { list, echo, update, removeBatchByKeys } from "@/api/feedback";
import { useAdminStore } from "@/stores/admin";
const data = ref([{}]);
const feedback = ref({
  key: null,
  userId: null,
  nickName: "",
  avatar: "",
  content: "",
});

const loading = ref(true);
const adminStore = useAdminStore();
onMounted(() => {
  setTimeout(() => {
    getAdmins();
    loading.value = false;
  }, 800);
});

const total = ref(0);
const pageNo = ref(1);
const pageSize = ref(10);
const isAdmin = ref(false);
if (adminStore.admin.role === "管理员") {
  isAdmin.value = true;
}
//异步请求管理员数据
function getAdmins() {
  list(pageNo.value, pageSize.value).then((res) => {
    if (res.code === 200 && res.data != null) {
      data.value = res.data.data;
      total.value = res.data.total;
    }
  });
}

//监听分页变化
function handlePageChange(param1, param2) {
  pageNo.value = param1;
  pageSize.value = param2;
  setTimeout(() => {}, 1000);
  getAdmins();
}

//新增管理员操作
function save() {
  formRef.value
    .validate()
    .then(() => {
      //修改反馈
      update(feedback.value).then((res) => {
        if (res.code === 200) {
          message.success("修改成功！");
          //重新加载管理员数据
          getAdmins();
          open.value = false;
          feedback.value = {};
        }
      });
    })
    .catch((error) => {
      console.log("error", error);
    });
}

//取消新增管理员操作
function handleCancel() {
  open.value = false;
  feedback.value = {};
}

//管理员修改操作
const open = ref(false);
function edit(id) {
  echo(id).then((res) => {
    if (res.code === 200 && res.data != null) {
      feedback.value = res.data;
      open.value = true;
    }
  });
}

//管理员确认删除回调操作
function remove(id) {
  removeBatchByKeys(id).then((res) => {
    if (res.code === 200) {
      message.success("删除成功！");
      //重新加载分页数据
      getAdmins();
    }
  });
}

//管理员取消删除回调操作
function cancel() {
  message.warning("取消删除！");
}

//数据校验
const formRef = ref();
const labelCol = {
  span: 5,
};
const wrapperCol = {
  span: 13,
};
const rules = {
  content: [
    {
      required: true,
      message: "请输入反馈内容",
      trigger: "change",
    },
  ],
};

//存储批量管理员id
const state = reactive({
  selectedRowKeys: [],
});
//当有一个或多个管理员被选中时触发按钮
const hasSelected = computed(() => state.selectedRowKeys.length > 0);
//批量删除管理员
const removeBachById = () => {
  setTimeout(() => {
    state.selectedRowKeys = [];
  }, 1000);
  //删除后做操作
  removeBatchByKeys(state.selectedRowKeys).then((res) => {
    if (res.code === 200) {
      message.success("删除成功！");
      //重新加载分页数据
      getAdmins();
    }
  });
};
//当选中行改变时触发
const onSelectChange = (selectedRowKeys) => {
  state.selectedRowKeys = selectedRowKeys;
};
</script>

<style scoped>
.options {
  margin-top: 10px;
}

.table {
  margin-top: 10px;
}

.avatar-uploader > .ant-upload {
  width: 50px;
  height: 50px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.paginAtion {
  padding-top: 20px;
  padding-left: 310px;
  margin-bottom: 20px;
}
</style>>